package com.bocai.kelalacar.ui.car.main.presenter;

import android.util.Log;

import com.bocai.kelalacar.base.BaseResponse;
import com.bocai.kelalacar.bean.CarDriverBean;
import com.bocai.kelalacar.bean.ModifyAddressBean;
import com.bocai.kelalacar.net.Api;
import com.bocai.kelalacar.ui.car.main.contract.MessageContract;
import com.bocai.kelalacar.ui.car.main.contract.ModifyAddressContract;
import com.bocai.kelalacar.util.AESUtils;
import com.bocai.kelalacar.util.RetryWithDelay;
import com.google.gson.Gson;

import javax.inject.Inject;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by yuanfei on 2017/8/25.
 */

public class ModifyAddressPresenter extends ModifyAddressContract.presenter {

    private final ModifyAddressContract.view view;
    private final Api api;
    public String start = "{\"content\":";
    public String end = "}";

    @Inject
    public ModifyAddressPresenter(ModifyAddressContract.view view, Api api) {
        this.view = view;
        this.api = api;
    }

    @Override
    public void changeAddress(String orderId) {
        view.showLoading("");
        api.changeAddress(orderId).subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResponse>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable", e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                view.stopLoading();
                Log.e("baseResponse", baseResponse.toString());
                Log.e("baseResponse", baseResponse.getReturnCode().equals("200") + "");
                if (baseResponse.getReturnCode().equals("200")) {
                    String result = AESUtils.decode(baseResponse.getData());
                    Log.e("result",result);
                    ModifyAddressBean bean = new Gson().fromJson(result,ModifyAddressBean.class);
                    view.getChangeAddress(bean);
                } else {
                    view.showErrorTip(baseResponse.getMsg());
                }
            }
        });
    }

    @Override
    public void disAgreeChange(String orderId) {
        view.showLoading("");
        api.disAgreeChange(orderId).subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResponse>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable", e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                view.stopLoading();
                Log.e("baseResponse", baseResponse.toString());
                Log.e("baseResponse", baseResponse.getReturnCode().equals("200") + "");
                if (baseResponse.getReturnCode().equals("200")) {
                    view.disAgreeChange();
                } else {
                    view.showErrorTip(baseResponse.getMsg());
                }
            }
        });
    }

    @Override
    public void agreeChange(String orderId) {
        view.showLoading("");
        api.agreeChange(orderId).subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResponse>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable", e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                view.stopLoading();
                Log.e("baseResponse", baseResponse.toString());
                Log.e("baseResponse", baseResponse.getReturnCode().equals("200") + "");
                if (baseResponse.getReturnCode().equals("200")) {
                    view.agreeChange();
                } else {
                    view.showErrorTip(baseResponse.getMsg());
                }
            }
        });
    }
}
